﻿using System;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace removeav2012
{
    public partial class automaticForm : Form
    {
        public automaticForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ncrReg = Application.StartupPath + "\\FixNCR.reg";

            listBox1.Items.Add("**Beginning Malware Removal**");
            progressBar1.Value = 10;

            killAndDelete();
            deletetemps();
            listBox1.Items.Add("**Completed Deletion**");

            progressBar1.Value = 25;

            if (File.Exists(ncrReg))
            {
                progressBar1.Value = 35;
                fixexe();
                progressBar1.Value = 50;
            }
            else
            {
                progressBar1.Value = 30;
                downloader("http://download.bleepingcomputer.com/reg/FixNCR.reg", "FixNCR.reg");
                progressBar1.Value = 35;
                fixexe();
                progressBar1.Value = 50;
            }

            listBox1.Items.Add("**Completed Malware Removal**");

            progressBar1.Value = 100;

            button2.Enabled = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.DefaultExt = "log";
            saveFileDialog.AddExtension = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileDialog.FileName);

                foreach (object item in listBox1.Items)
                {
                    sw.WriteLine(item.ToString());
                }
                sw.Close();
            }

        }

        public void killAndDelete()
        {
            string[] ladexefilenames = Directory.GetFiles(Environment.GetEnvironmentVariable("LOCALAPPDATA"), "???.exe");
            string[] ladfilenames = Directory.GetFiles(Environment.GetEnvironmentVariable("LOCALAPPDATA"), "*.");
            string[] aupfilenames = Directory.GetFiles(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"), "*.");
            string[] adfilenames = Directory.GetFiles(Environment.GetEnvironmentVariable("APPDATA"), "*.");

            foreach (string file in ladexefilenames)
            {
                getandkill(file);
                deleteFile(file);
                listBox1.Items.Add("Deleted: " + file);
            }


            foreach (string file in ladfilenames)
            {
                deleteFile(file);
                listBox1.Items.Add("Deleted: " + file);
            }


            foreach (string file in aupfilenames)
            {
                deleteFile(file);
                listBox1.Items.Add("Deleted: " + file);
            }


            foreach (string file in adfilenames)
            {
                deleteFile(file);
                listBox1.Items.Add("Deleted: " + file);
            }

        }

        private void getandkill(string processname)     //class to kill processes
        {
            string bProc = Path.GetFileNameWithoutExtension(processname); //strip the path and extension
            Process[] processes = Process.GetProcessesByName(bProc); //find all processes by that name

            if (processes.Length > 0)
            {
                foreach (Process process in processes)      //go thru the array and kill each process
                {
                    process.Kill();
                }
            }
        }

        private void deleteFile(string file)
        {
            if (System.IO.File.Exists(file)) //check to make sure file exists
            {
                // Use a try block to catch IOExceptions, to
                // handle the case of the file already being
                // opened by another process.
                try
                {
                    getandkill(file); //kill it first. helps to make sure file can be deleted
                    System.IO.File.Delete(file); //delete the bad file.
                }
                catch (System.IO.IOException errorout) // if file cant be deleted, display error
                {
                    Console.WriteLine(errorout);
                    return;
                }
            }
        }

        private void deletetemps()
        {
            Process inetTemp = Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8");  // Delete temps
            inetTemp.WaitForExit();
            listBox1.Items.Add("**Deleted Temporary Internet Files**");

            StreamWriter sw = new StreamWriter("deltemp.bat"); // This section creates a batch file to delete the temp files and folders recursively
            sw.WriteLine("del /q /s /f %temp%");
            sw.Close();

            Process delTemp = Process.Start("deltemp.bat"); //run the batch file to delete %TEMP%
            delTemp.WaitForExit();  //wait for it to finish
            listBox1.Items.Add("**Cleared Temp Files**");
            File.Delete("deltemp.bat"); //delete the batch file
        }

        private void fixexe()
        {
            string ncrReg = Application.StartupPath + "\\FixNCR.reg";         //path to FixNCR.Reg

            if (File.Exists(ncrReg))
            {
                Process regeditProcess = Process.Start("regedit.exe", " /s \"" + ncrReg + "\""); //insert registry fix for exe's
                regeditProcess.WaitForExit();

                listBox1.Items.Add("**Completed EXE Fix**");
            }
            else
            {
                downloader("http://download.bleepingcomputer.com/reg/FixNCR.reg", "FixNCR.reg");
            }
        }

        private void downloader(string url, string filename)
        {
            string aPath = Application.StartupPath;     //find the path the app is being run from
            WebClient client = new WebClient();

            // Starts the download  

            client.DownloadFile(new Uri(url), aPath + "\\" + filename);
            listBox1.Items.Add("**Downloaded: " + filename + "**");
        }



    }
}
